package logic

import (
	"core/models/entity"
	"core/service"
	"framework/msError"
	"framework/remote"
	"game/componet/room"
	"game/models/request"
	"sync"
)

type Union struct {
	sync.RWMutex
	Id         int64
	m          *UnionManager
	RoomList   map[string]*room.Room
	activeTime int64
}

func (u *Union) CreateRoom(service *service.UserService, session *remote.Session, req request.CreateRoomReq, userData *entity.User) *msError.Error {
	//1.创建一个房间 生成一个房间号
	roomId := u.m.CreateNewRoomID()
	newRoom := room.NewRoom(roomId, req.UnionID, req.GameRule, u)
	u.RoomList[roomId] = newRoom
	//2.将房间号 推送给客户端
	//3.将游戏类型 推送给客户端（用户进入游戏的推送）
	return newRoom.UserEntryRoom(session, userData)
}

func NewUnion(id int64, m *UnionManager) *Union {
	return &Union{
		Id:       id,
		m:        m,
		RoomList: make(map[string]*room.Room),
	}
}

func (u *Union) DismissRoom(roomId string) {
	u.Lock()
	defer u.Unlock()
	delete(u.RoomList, roomId)

}
